<?php 
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 用户模型
 */
class User_model extends CI_Model{

	public function __construct(){

		parent::__construct();

	}
	
	/**
	 * 搜索
	 */
	public function member_list_one($value) {
		$sql = "SELECT * FROM pc_ddth_user WHERE (mobile = '$value' or user_name = '$value' or user_account = '$value') AND user_account <> 'dedaozhe'";
		
		$query = $this->db->query ( $sql );
		
		return $query->result_array ();
	}
	
	/**
	 * 按id更新用户信息及公益币
	 */
	public function update_userinfo_gyb($id) {
		$data = array (
				'user_account' => $_POST ['user_account'],
				'user_name' => $_POST ['user_name'],
				'zfb_account' => $_POST ['zfb_account'],
				'bank_name' => $_POST ['bank_name'],
				'bank_card' => $_POST ['bank_card'],
				'wx_id' => $_POST ['wx_id'],
				'public_money' => $_POST ['gyb'],
				'jt_money' => $_POST ['jt'],
				'tg_money' => $_POST ['tg'],
				'kick_status' => $_POST['dongjie']
		);
		if (! empty ( $_POST ['pwd'] )) {
			$data ['pwd'] = md5 ( $_POST ['pwd'] );
		}
		if (! empty ( $_POST ['spwd'] )) {
			$data ['spwd'] = md5 ( $_POST ['spwd'] );
		}
		
		$result = $this->db->update ( 'pc_ddth_user', $data, array (
				'id' => $id 
		) );
		
		return $result;
	}
	
	/**
	 * 查询所有
	 */
	public function member_list() {
		$query = $this->db->get ( 'pc_ddth_user' );
		return $query->result_array ();
	}
	
	/**
	 * 按手机号更新密码
	 */
	public function update_pwd_by_mobile($mobile,$pwd_type) {
		if($pwd_type == 'pwd')
		{
			$data['pwd'] = md5 ( $_POST ['new_pwd'] );
		}
		else
		{
			$data['spwd'] = md5 ( $_POST ['new_pwd'] );
		}
		
		$result = $this->db->update ( 'pc_ddth_user', $data, array (
				'mobile' => $mobile 
		) );
		
		return $result;
	}
	
	/**
	 * 按id更新公益币
	 */
	public function update_gongyibi_num($id, $number) {
		$data = array (
				'public_money' => $number 
		);
		
		$result = $this->db->update ( 'pc_ddth_user', $data, array (
				'id' => $id 
		) );
		
		return $result;
	}

	/**
	 * 注册
	 */
	public function user_insert($pid){

		$data = array(
			'user_account' => $_POST['user_account'],
			'up_account'   => $_POST['up_account'],
			'pwd'          => md5($_POST['pwd']),
			'spwd'         => md5($_POST['spwd']),
			'user_name'    => $_POST['user_name'],
			'mobile'       => $_POST['mobile'],
			'zfb_account'  => $_POST['zfb_account'],
			'bank_name'    => $_POST['bank_name'],
			'bank_card'    => $_POST['bank_card'],
			'wx_id'        => $_POST['wx_id'],
			'create_time'  => time(),
			'pid'          => $pid,
		);

		$result = $this->db->insert('pc_ddth_user', $data);

		if($result)
		{
			return $this->db->insert_id();
		}
		else
		{
			return 0;	
		}
		
	}

	/**
	 * 更新树地址
	 */
	public function update_path($id,$path)
	{
		$data['path'] = $path . $id.'/';

		$result = $this->db->update('pc_ddth_user', $data,array('id' =>$id));

		return $result;
	}

	public function get_user_mobile($mobile)
	{
		$sql = "SELECT * FROM pc_ddth_user WHERE mobile = $mobile";

		$query = $this->db->query($sql);
//		echo $sql;
		return $query->row_array();
	}

	/**
	 * 按账号查询用户
	 */
	public function get_uesr_info($user_account)
	{

		$sql = "SELECT * FROM pc_ddth_user WHERE user_account = '$user_account'";

		$query = $this->db->query($sql);

		return $query->row_array();
	}

	/**
	 * 按id获取用户信息
	 */
	public function get_user_info_id($id)
	{
		$sql = "SELECT * FROM pc_ddth_user WHERE id = $id";

		$query = $this->db->query($sql);

		return $query->row_array();
	}

	/**
	 * 按账号密码查询用户
	 */
	public function check_user($acc,$pwd)
	{
		$sql = "SELECT * FROM pc_ddth_user WHERE user_account = '$acc' AND pwd = '$pwd'";

		$query = $this->db->query($sql);

		return $query->row_array();

	}

	/**
	 * 按账号二级密码查询用户
	 */
	public function check_spwd($id,$spwd)
	{
		$sql = "SELECT * FROM pc_ddth_user WHERE id = $id AND spwd = '$spwd'";

		$query = $this->db->query($sql);

		return $query->row_array();

	}

	/**
	 * 按id更新用户信息
	 */
	public function update_userinfo($id)
	{
		$data = array(
			'user_name' => $_POST['user_name'],
			'mobile' => $_POST['mobile'],
			'zfb_account' => $_POST['zfb_account'],
			'bank_name' => $_POST['bank_name'],
			'bank_card' => $_POST['bank_card'],
			'wx_id' => $_POST['wx_id'],
		);

		$result = $this->db->update('pc_ddth_user', $data,array('id' =>$id));

		return $result;
	}

	/**
	 * 更新积分
	 */
	public function update_score($score,$id)
	{
		$data['score'] = $score;

		$result = $this->db->update('pc_ddth_user', $data,array('id' =>$id));

		return $result;
	}
	/**
	 * 密码修改
	 * type 1 一级密码修改
	 * type 2 二级密码修改
	 */
	public function update_pwd($id,$pwd,$type)
	{
		if($type == 1)
		{
			$data['pwd'] = $pwd;
		}
		elseif($type == 2)
		{
			$data['spwd'] = $pwd;
		}
		else
		{
			return FALSE;
		}

		$result = $this->db->update('pc_ddth_user', $data,array('id' =>$id));

		return $result;
	}

	/**
	 * 公益币变更
	 */
	public function update_public_money($id,$money){

		$data['public_money'] = $money;

		$result = $this->db->update('pc_ddth_user',$data,array('id'=>$id));

		return $result;
	}

	/**
	 *按推荐人获取会员列表
	 */
	public function get_member_list_up($up_account)
	{
		$sql = "SELECT * FROM pc_ddth_user WHERE up_account = '$up_account'";

		$query = $this->db->query($sql);

		return $query->result_array();
	}

	/**
	 * 静态金额更新
	 */
	public function update_user_jt_money($money,$id)
	{
		$sql = "UPDATE pc_ddth_user SET jt_money = jt_money + $money";

		$where = " WHERE id = $id";

		$sql .= $where;

		$result = $this->db->query($sql);
		//echo $this->db->last_query();
		return $result;
	}

	/**
	 * 获取团队等级——得道者/天时/地利/人和/君子
	 */
	private function get_team_lv($num,$acc)
	{

		if($num <= 4)
		{
			$lv = 1;
		}
		elseif($num > 4 && $num <= 29)
		{
			$lv = 2;
		}
		else
		{
			//默认最低是天时2
			$lv = 2;

			if($num >= 30)
			{
				$ts_num = $this->get_team_tiaojian($acc,2);
				//2天时
				if($ts_num >= 2)
				{
					$lv = 3;//地利
				}
			}

			if($num >= 99)
			{
				$dl_num = $this->get_team_tiaojian($acc,3);
				//2地利
				if($dl_num >= 2)
				{
					$lv = 4;//人和
				}
			}
			//
			if($num >= 500)
			{
				$rh_num = $this->get_team_tiaojian($acc,4);
				//2个人和
				if($rh_num >= 2)
				{
					$lv = 5;
				}
			}
		}

		return $lv;
	}

	/**
	 * 获取地利、人和、君子必要条件,直推下属2人
	 */
	private function get_team_tiaojian($up_account,$lv)
	{
		//直属等级大于传入等级的个数
		$sql = "SELECT count(id) AS num FROM pc_ddth_user WHERE up_account='$up_account' AND team_lv >= $lv AND kick_status = 0";

		$query = $this->db->query($sql);

		$row = $query->row();

		$num = 0;

		if (isset($row))
		{
		    $num = $row->num;
		}

		return $num;

	}

	/**
	 * 更新用户团队等级、人数
	 */
	private function user_team_update($team_lv,$id)
	{
		if(!empty($team_lv))
		{
			//$data['team_lv'] = $team_lv;
			$sql = "UPDATE pc_ddth_user SET team_lv = $team_lv , team_num = team_num + 1";
		}
		else
		{
			$sql = "UPDATE pc_ddth_user SET team_num = team_num + 1";
		}
		$where = " WHERE id = $id";

		$sql .= $where;
		//加一
		$result = $this->db->query($sql);
		//echo $this->db->last_query();
		return $result;
	}



	/**
	 * 每注册一个帐号，递归上级帐号一直到不存在
	 */
	public function reg_digui_up_account($up_account)
	{
		//echo $up_account;
		$user_info = $this->get_uesr_info($up_account);
		//var_dump($user_info);
		if(empty($user_info))
		{
			//无用户退出递归
			return TRUE;
		}
		else
		{
			$user_info['team_num'] += 1;
			//var_dump($user_info);
			//echo $user_info['team_num'];
			$lv = $this->get_team_lv($user_info['team_num'],$user_info['user_account']);
			//echo $lv .'<='.$user_info['team_lv'];
			if($lv <= $user_info['team_lv'])
			{
				$lv = 0;//$user_info['team_lv'];
			}
			//echo '------>'.$lv.'<br>';
			//更新用户团队等级和人数
			$this->user_team_update($lv,$user_info['id']);

			$this->reg_digui_up_account($user_info['up_account']);
		}
	}

	/**
	 * 减少钱包金额
	 */
	public function update_minus_user_money($type,$money,$id)
	{
		if($type == 0)
		{
			//$data['team_lv'] = $team_lv;
			$sql = "UPDATE pc_ddth_user SET jt_money = jt_money - $money";
		}
		else
		{
			$sql = "UPDATE pc_ddth_user SET tg_money = tg_money - $money";
		}
		$where = " WHERE id = $id";

		$sql .= $where;

		$result = $this->db->query($sql);
		//echo $this->db->last_query();
		return $result;
	}

	/**
	 * 用户更新推广钱包
	 */
	public function update_user_tgmoney($money,$id)
	{
		$sql = "UPDATE pc_ddth_user SET tg_money = tg_money + $money  WHERE id = $id";

		$result = $this->db->query($sql);
		//echo $this->db->last_query();
		return $result;

	}

	// public function get_children_id($up_account,$id_arr = array())
	// {

	// 	$list = $this->get_member_list_up($up_account);
	// 	//var_dump($list);
	// 	if(!empty($list))
	// 	{
	// 		foreach ($list as $k => $v) {

	// 			$id_arr[] = $v['id'];

	// 			$this->get_children_id($v['user_account'],$id_arr);
	// 		}
	// 	}
		
	// 	//var_dump($id_arr);
	// 	return $id_arr;
		
	// }

	/**
	 * 封号
	 */
	public function kick_user($id)
	{
		$data['kick_status'] = 1;

		$result = $this->db->update ( 'pc_ddth_user', $data, array (
				'id' => $id 
		) );
		$this->down_lv($id);
		//echo $this->db->last_query();
		return $result;
	}

	/**
	 * 封号后降级
	 */
	public function down_lv($id){

		$user_info = $this->get_user_info_id($id);

		if(!empty($user_info)){
			$path = $user_info['path'];
			//节点地址
			$path_arr = explode('/', $path); //implode('/', $path);
			//去掉0，自身和多余的
			$arr_num = count($path_arr);
			unset($path_arr[$arr_num-1]);
			unset($path_arr[$arr_num-2]);
			unset($path_arr[0]);
			$path_arr = array_reverse($path_arr);
			//var_dump($path_arr);
			//echo '<br>';
			$in_str = implode(',', $path_arr);

			$sql = "SELECT id,user_account,team_lv,team_num FROM pc_ddth_user WHERE id in ($in_str)";
			//echo $sql;
			$result = $this->db->query($sql)->result_array();

			$sort_arr = array();
			foreach ($path_arr as $va) {
				foreach ($result as $v) {
				
					if($va == $v['id'])
					{
						$sort_arr[$va] = $v;
					}
				}
			}
	        //$sort_arr = array_reverse($sort_arr);

	        //var_dump($sort_arr);
	        foreach ($sort_arr as $val) {
	        	$sql = '';

	        	$new_lv = $this->get_team_lv($val['team_num'],$val['user_account']);
	        	//封号更新上级LV
	        	$sql = "UPDATE pc_ddth_user SET team_lv = ".$new_lv." WHERE id = ".$val['id'];
	        	//echo $sql;
				$this->db->query($sql);
				//echo '<br>';
	        }
		}
	}

	/**
	 * 封号更新上级LV
	 */
	// public function update_user_team_lv($id,$lv){

	// }

}
